package com.css.fxfzypg.manage.sampling.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.manage.sampling.repository.SamplingReqository;
import com.css.fxfzypg.manage.sampling.repository.entity.SamplingEntity;
import com.css.fxfzypg.manage.sampling.service.SamplingService;
import com.css.fxfzypg.manage.sampling.vo.SamplingParams;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.PlatformSessionUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SamplingServiceImpl implements SamplingService {
    @Autowired
    SamplingReqository samplingReqository;

    @Autowired
    SysAreaService sysAreaService;

    @Override
    public boolean insertSurvey(SamplingEntity samplingEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        samplingEntity.setId(uuid);
        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(samplingEntity.getProvince(), "UTF-8"), URLEncoder.encode(samplingEntity.getCity(), "UTF-8"), URLEncoder.encode(samplingEntity.getArea(), "UTF-8"));
        samplingEntity.setProvinceId(divisionIds.split("_")[0]);
        samplingEntity.setCityId(divisionIds.split("_")[1]);
        samplingEntity.setAreaId(divisionIds.split("_")[2]);
        samplingEntity.setCreateTime(new Date());
        samplingEntity.setIsSubmit("0");
        samplingEntity.setDelFlag("0");
        return samplingReqository.insertSurvey(samplingEntity);
    }

    @Override
    public boolean saveAndSubmit(SamplingEntity samplingEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        samplingEntity.setId(uuid);
        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(samplingEntity.getProvince(), "UTF-8"), URLEncoder.encode(samplingEntity.getCity(), "UTF-8"), URLEncoder.encode(samplingEntity.getArea(), "UTF-8"));
        samplingEntity.setProvinceId(divisionIds.split("_")[0]);
        samplingEntity.setCityId(divisionIds.split("_")[1]);
        samplingEntity.setAreaId(divisionIds.split("_")[2]);
        samplingEntity.setDelFlag("0");
        samplingEntity.setIsSubmit("1");
        samplingEntity.setCreateTime(new Date());
        samplingEntity.setSubmitter(samplingEntity.getCreateName());
        samplingEntity.setSubmitterId(samplingEntity.getCreateId());
        samplingEntity.setSubmitTime(new Date());
        return samplingReqository.insertSurvey(samplingEntity);
    }

    @Override
    public  Map<String,Object> querySubmitOrNot(String submitFlag,String taskCode, int curPage, int pageSize ,String userId) {
        return samplingReqository.querySubmitOrNot(submitFlag,taskCode,curPage,pageSize,userId);
    }

    @Override
    public boolean updateIsSubmit(Map<String,String> map) {
        return samplingReqository.updateIsSubmit(map);
    }

    @Override
    public SamplingEntity queryData(String id) {
        return samplingReqository.queryData(id);
    }

    @Override
    public boolean modifyData(SamplingEntity samplingEntity) {
        return samplingReqository.modifyData(samplingEntity);
    }

    @Override
    public boolean modifyAndSubmit(SamplingEntity samplingEntity) {
        samplingEntity.setIsSubmit("1");
        samplingEntity.setSubmitter(samplingEntity.getCreateName());
        samplingEntity.setSubmitterId(samplingEntity.getCreateId());
        samplingEntity.setSubmitTime(new Date());
        return samplingReqository.modifyData(samplingEntity);
    }

    @Override
    public Map<String, Object> querySamplingData(SamplingParams queryParams, int curPage, int pageSize) {
        Map<String, Object> map = samplingReqository.querySamplingData(queryParams,curPage,pageSize);
        return map;
    }

    @Override
    public boolean deleteSampling(String ids) {
        boolean res = true;
        for (String id : ids.split(",")) {
            res = samplingReqository.deleteSampling(id);
            if(res == false){
                break;
            }
        }
        return res;
    }



}
